package org.budo.dubbo.protocol.http.view.render.budo;

import org.budo.dubbo.protocol.http.authentication.AuthenticationCheckService.Fail;
import org.budo.dubbo.protocol.http.exception.remoting.AuthenticationCheckException;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author lmw
 */
public class AuthenticationCheckExceptionRender {
    private static final Logger log = Slf4j.getLogger();

    public ApiError buildApiError(AuthenticationCheckException exception) {
        Fail fail = exception.getFail();

        if (null == fail) {
            log.error("#19 authenticationCheck failed, exception=" + exception, exception);
            return ApiError.AUTHENTICATION_FAILED;
        }

        if (fail.equals(Fail.TOKEN_EMPTY)) {
            return ApiError.TOKEN_EMPTY;
        }

        if (fail.equals(Fail.TOKEN_NOT_FOUND)) {
            return ApiError.TOKEN_NOT_FOUND;
        }

        if (fail.equals(Fail.TOKEN_EXPIRED)) {
            return ApiError.TOKEN_EXPIRED;
        }

        return ApiError.AUTHENTICATION_FAILED;
    }
}